$('.back').click(function () {
    window.history.back();
})
var imgJson = {
    idCard: [],
    school: [],
    xuexin: [],
    studentCard: [],
    alipay: [],
    bank: []
}
function clearFile(id) {
    var file = $('#' + id);
    $('#' + id).val('')
    $('.canver').hide();
}
function makeHtml(arr, tag) {
    console.log(imgJson);
    var html = ''
    if (arr.length >= 1) {
        arr.forEach(function (ele, index, arr) {
            html += '<div class="shangchuanfujian-imgBox"><img src="' + ele + '"><div class="img-del" data-index="' + index + '" data-tag="' + tag + '">X</div></div>'
        })
    }
    $('#' + tag + 'Box').html(html);
}
function checkNum(tag) {
    if (imgJson[tag].length >= $('.' + tag + 'Input').data('max')) {
        return false;
    } else {
        return true;
    }
}
$('.inputfile').on('change', function () {
    var _this = $(this);
    var tag = _this.data('tag')
    if (checkNum(tag)) {
        var data = new FormData();
        data.append('file', document.getElementById('inputfile' + tag).files[0]);
        $('.canver').show();
        $.ajax({
            url: 'http://192.168.1.115:8080/muser/uploadFile',
            method: 'POST',
            contentType: false,
            dataType: 'json',
            processData: false,
            data: data,
            success: function (result) {
                console.log(11111, tag);
                var imgArr = imgJson[tag]
                imgArr.push(result.data.url);
                makeHtml(imgArr, tag);

                if (result.code == 0) {
                    clearFile(_this.attr('id'));
                } else {
                    alert(result.msg);
                    clearFile(_this.attr('id'));
                }
            },
            error: function (xhr, type) {
                clearFile(_this.attr('id'));
            }
        })
    } else {
        clearFile(_this.attr('id'));
        alert('上传图片过多!');
    }
})

$(document).on('click', '.img-del', function () {
    var _this = $(this);
    var tag = _this.data('tag');
    var index = _this.data('index');
    imgJson[tag].splice(index, 1);
    makeHtml(imgJson[tag], tag);
})

$('.submit').click(function () {

    if (imgJson['xuexin'].length <= 1) {
        alert('雪心不能小于1');
        return;
    }

    var data = {};
    for (item in imgJson) {
        data[item] = imgJson[item].join(',')
    }
    $.ajax({
        url: 'http://192.168.1.115:8080/mobile/loanApplication/studentLoan/uploadData',
        method: 'POST',
        dataType: 'json',
        data: {
            loanApplicationId: 1,
            uploadData_identificationUrl: data['idCard'],
            uploadData_campusCardUrl: data['school'],
            uploadData_xuexinUrl: data['xuexin'],
            uploadData_studentCardUrl: data['studentCard'],
            uploadData_alipayWaterUrl: data['alipay'],
            uploadData_bankWaterUrl: data['bank']
        },
        success: function (result) {
            if (result.code == 0) {
                alert('ok');
            } else {
                alert(result.desc);
            }
        },
        error: function (xhr, type) {
            alert('服务器错误');
        }
    })
});
